import { gcj02towgs84 } from "@/utils/CoordinateConvert/gcj02towgs84";
import Emberbox from "@/utils/mapbox/engine";

export default function addBuildings(emberbox: Emberbox, source: any) {
  // console.log("addBuildings Source", source);

  source.features.forEach((item: any) => {
    item.geometry.coordinates = item.geometry.coordinates[0];
    item.geometry.coordinates[0].forEach((sets: any) => {
      let asets = gcj02towgs84(sets);
      sets[0] = asets[0];
      sets[1] = asets[1];
    });
    item.geometry.type = "Polygon";
  });

  let data: any = null,
    buildings: any = null,
    isShow: boolean = true;

  data = source;

  buildings = emberbox.geojsonManage?.addIndoorJson(source);
  isShow = true;
  //       isShow = true;
  //   fetch(`${baseurl}/buildings`)
  //     .then((res) => res.json())
  //     .then((res: any) => {
  //       data = res;
  //       buildings = emberbox.geojsonManage?.addIndoorJson(res);
  //       isShow = true;
  //       console.log("buildings", buildings);
  //     })
  //     .catch((err) => {
  //       console.log("数据获取失败：buildings", err);
  //     });

  emberbox.on("onZoom", (e: number) => {
    if (data === null) return;
    if (e > 18.3 && isShow) {
      isShow = false;
      emberbox.geojsonManage?.removeJson(buildings);
      buildings = null;
    }

    if (e < 18 && isShow === false) {
      isShow = true;
      buildings = emberbox.geojsonManage?.addIndoorJson(data);
    }
  });
}
